﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace SPForumWP.ForumWP
{
    public partial class ForumWPUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //if (Session["Id"] == null || Session["Id"].ToString() == "")
                //{
                //    Session["Id"] = 1;
                //}
                //if (Session["lang"] == null || Session["lang"].ToString() == "")
                //{
                //    Session["lang"] = "EN";
                //    Session["dir"] = "ltr";
                //}
                //Response.Write(SPContext.Current.Web.CurrentUser.LoginName);
                //return;
                MessageBoard.User currentUser = MessageBoard.User.Get(SPContext.Current.Web.CurrentUser.LoginName);
                MessageBoard.Language currentLanguage = MessageBoard.Language.Get("EN");

                String pageName = "Home.aspx";

                // GroupList
                if (Request.QueryString["Forum"] == null || Request.QueryString["Forum"] == "GroupList")
                {
                    MessageBoardControl.GroupList groupList = new MessageBoardControl.GroupList();
                    groupList.URL = pageName + "?";
                    groupList.MessageBoardSecurityUrl = pageName + "?";
                    groupList.GroupSecurityUrl = pageName + "?";
                    groupList.AddURL = pageName + "?";
                    groupList.EditURL = pageName + "?";
                    groupList.DeleteURL = pageName + "?";
                    groupList.CurrentUser = currentUser;
                    groupList.ImagesFolder = "/Images/Forum";
                    groupList.Language = currentLanguage;
                    contentDiv.Controls.Add(groupList);
                }

                // AddGroup
                if (Request.QueryString["Forum"] == "AddGroup")
                {
                    MessageBoardControl.AddGroup addGroup = new MessageBoardControl.AddGroup();
                    addGroup.URLAfter = pageName;
                    addGroup.CurrentUser = currentUser;
                    addGroup.Language = currentLanguage;
                    contentDiv.Controls.Add(addGroup);
                }

                //EditGroup
                if (Request.QueryString["Forum"] == "EditGroup")
                {
                    MessageBoardControl.EditGroup editGroup = new MessageBoardControl.EditGroup();
                    editGroup.Group = MessageBoard.Group.Get(int.Parse(Request.QueryString["GroupId"]));
                    editGroup.URLAfter = pageName;
                    editGroup.Language = currentLanguage;
                    contentDiv.Controls.Add(editGroup);
                }

                ////DeleteGroup
                //If Request.QueryString("Forum") = "DeleteGroup" Then
                //    Dim deleteGroup As MessageBoardControl.DeleteGroup = New MessageBoardControl.DeleteGroup
                //    deleteGroup.Group = MessageBoard.Group.Get(CInt(Request.QueryString("GroupId")))
                //    deleteGroup.URLAfter = pageName
                //    deleteGroup.Language = currentLanguage
                //    contentDiv.Controls.Add(deleteGroup)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.URLMessageBoard = pageName
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                //CategoryList
                if (Request.QueryString["Forum"] == "CategoryList")
                {
                    MessageBoardControl.CategoryList categoryList = new MessageBoardControl.CategoryList();
                    categoryList.URL = pageName + "?";
                    categoryList.Group = MessageBoard.Group.Get(int.Parse(Request.QueryString["GroupId"]));
                    categoryList.CurrentUser = currentUser;
                    categoryList.ImagesFolder = "/Images/Forum";
                    categoryList.CategorySecurityUrl = pageName + "?";
                    categoryList.ForumSecurityUrl = pageName + "?";
                    categoryList.AddCategoryURL = pageName + "?";
                    categoryList.EditCategoryURL = pageName + "?";
                    categoryList.DeleteCategoryURL = pageName + "?";
                    categoryList.AddForumURL = pageName + "?";
                    categoryList.EditForumURL = pageName + "?";
                    categoryList.DeleteForumURL = pageName + "?";
                    categoryList.Language = currentLanguage;
                    contentDiv.Controls.Add(categoryList);
                }

                //AddCategory
                if (Request.QueryString["Forum"] == "AddCategory")
                {
                    MessageBoardControl.AddCategory addCategory = new MessageBoardControl.AddCategory();
                    addCategory.URLAfter = pageName + "?Forum=CategoryList&GroupId=" + Request.QueryString["GroupId"];
                    addCategory.Group = MessageBoard.Group.Get(int.Parse(Request.QueryString["GroupId"]));
                    addCategory.Language = currentLanguage;
                    addCategory.CurrentUser = currentUser;
                    contentDiv.Controls.Add(addCategory);
                }

                //// EditCategory
                //If Request.QueryString("Forum") = "EditCategory" Then
                //    Dim editCategory As MessageBoardControl.EditCategory = New MessageBoardControl.EditCategory
                //    editCategory.URLAfter = pageName + "?Forum=CategoryList&GroupId=" & Request.QueryString("GroupId")
                //    editCategory.Category = MessageBoard.Category.Get(CInt(Request.QueryString("CategoryId")))
                //    editCategory.Language = currentLanguage
                //    editCategory.CurrentUser = currentUser
                //    contentDiv.Controls.Add(editCategory)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.URLMessageBoard = pageName
                //    tree.Group = MessageBoard.Category.Get(CInt(Request.QueryString("CategoryId"))).Group
                //    tree.URLGroup = pageName + "?GroupId=" & tree.Group.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                //// Delete Category
                //If Request.QueryString("Forum") = "DeleteCategory" Then
                //    Dim deleteCategory As MessageBoardControl.DeleteCategory = New MessageBoardControl.DeleteCategory
                //    deleteCategory.URLAfter = pageName + "?Forum=CategoryList&GroupId=" & Request.QueryString("GroupId")
                //    deleteCategory.Category = MessageBoard.Category.Get(CInt(Request.QueryString("CategoryId")))
                //    deleteCategory.Language = currentLanguage
                //    contentDiv.Controls.Add(deleteCategory)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.URLMessageBoard = pageName
                //    tree.Group = MessageBoard.Category.Get(CInt(Request.QueryString("CategoryId"))).Group
                //    tree.URLGroup = pageName + "?GroupId=" & tree.Group.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                // AddForum
                if (Request.QueryString["Forum"] == "AddForum")
                {
                    MessageBoardControl.AddForum addForum = new MessageBoardControl.AddForum();
                    addForum.Category = MessageBoard.Category.Get(int.Parse(Request.QueryString["CategoryId"]));
                    addForum.CurrentUser = currentUser;
                    addForum.URLAfter = pageName + "?Forum=CategoryList&GroupId=" + addForum.Category.Group.Id.ToString();
                    addForum.Language = currentLanguage;
                    contentDiv.Controls.Add(addForum);
                }

                // EditForum
                if (Request.QueryString["Forum"] == "EditForum")
                {
                    MessageBoardControl.EditForum editForum = new MessageBoardControl.EditForum();
                    editForum.URLAfter = pageName + "?Forum=CategoryList&GroupId=" + Request.QueryString["GroupId"];
                    editForum.Forum = MessageBoard.Forum.Get(int.Parse(Request.QueryString["ForumId"]));
                    editForum.Language = currentLanguage;
                    contentDiv.Controls.Add(editForum);
                }

                //// DeleteForum
                //If Request.QueryString("Forum") = "DeleteForum" Then
                //    Dim deleteForum As MessageBoardControl.DeleteForum = New MessageBoardControl.DeleteForum
                //    deleteForum.URLAfter = pageName + "?Forum=CategoryList&GroupId=" & Request.QueryString("GroupId")
                //    deleteForum.Forum = MessageBoard.Forum.Get(CInt(Request.QueryString("ForumId")))
                //    deleteForum.Language = currentLanguage
                //    contentDiv.Controls.Add(deleteForum)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.Group = deleteForum.Forum.Category.Group
                //    tree.URLMessageBoard = pageName
                //    tree.URLGroup = pageName + "?GroupId=" & tree.Group.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                // ThreadList
                if (Request.QueryString["Forum"] == "ThreadList")
                {
                    MessageBoardControl.ThreadList threadList = new MessageBoardControl.ThreadList();
                    threadList.URL = pageName + "?";
                    threadList.ForumId = int.Parse(Request.QueryString["ForumId"]);
                    threadList.CurrentUser = currentUser;
                    threadList.ImagesFolder = "/Images/Forum";
                    threadList.Language = currentLanguage;
                    contentDiv.Controls.Add(threadList);

                    MessageBoardControl.AddThread addThread = new MessageBoardControl.AddThread();
                    addThread.URLAfter = pageName + "?Forum=ThreadList&ForumId=" + Request.QueryString["ForumId"];
                    addThread.Forum = MessageBoard.Forum.Get(int.Parse(Request.QueryString["ForumId"]));
                    addThread.ImagesFolder = "/Images/Forum";
                    addThread.CurrentUser = currentUser;
                    addThread.Language = currentLanguage;
                    content2Div.Controls.Add(addThread);
                }

                //// EditThread
                //If Request.QueryString("Forum") = "EditThread" Then
                //    Dim editThread As MessageBoardControl.EditThread = New MessageBoardControl.EditThread
                //    editThread.URLAfter = pageName + "?Forum=PostList&ThreadId=" & Request.QueryString("ThreadId")
                //    editThread.Thread = MessageBoard.Thread.Get(CInt(Request.QueryString("ThreadId")))
                //    editThread.ImagesFolder = "Images/Forum"
                //    editThread.CurrentUser = currentUser
                //    editThread.Language = currentLanguage
                //    content2Div.Controls.Add(editThread)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.Thread = editThread.Thread
                //    tree.URLMessageBoard = pageName
                //    tree.URLGroup = pageName + "?Forum=CategoryList&GroupId=" & tree.Thread.Forum.Category.Group.Id
                //    tree.URLCategory = pageName + "?"
                //    tree.URLForum = pageName + "?Forum=ThreadList&ForumId=" & tree.Thread.Forum.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                //// DeleteThread
                //If Request.QueryString("Forum") = "DeleteThread" Then
                //    Dim deleteThread As MessageBoardControl.DeleteThread = New MessageBoardControl.DeleteThread
                //    Dim thread As MessageBoard.Thread = MessageBoard.Thread.Get(CInt(Request.QueryString("ThreadId")))
                //    deleteThread.URLAfter = pageName + "?Forum=ThreadList&ForumId=" & thread.Forum.Id
                //    deleteThread.Thread = thread
                //    deleteThread.CurrentUser = currentUser
                //    deleteThread.Language = currentLanguage
                //    content2Div.Controls.Add(deleteThread)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.Thread = thread
                //    tree.URLMessageBoard = pageName
                //    tree.URLGroup = pageName + "?Forum=CategoryList&GroupId=" & tree.Thread.Forum.Category.Group.Id
                //    tree.URLCategory = pageName + "?"
                //    tree.URLForum = pageName + "?Forum=ThreadList&ForumId=" & tree.Thread.Forum.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                //// LockThread
                //If Request.QueryString("Forum") = "LockThread" Then
                //    Dim lockThread As MessageBoardControl.LockThread = New MessageBoardControl.LockThread
                //    lockThread.URLAfter = pageName & "?Forum=PostList&ThreadId=" & Request.QueryString("ThreadId")
                //    lockThread.Thread = MessageBoard.Thread.Get(CInt(Request.QueryString("ThreadId")))
                //    lockThread.CurrentUser = currentUser
                //    lockThread.Language = currentLanguage
                //    content2Div.Controls.Add(lockThread)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.Thread = lockThread.Thread
                //    tree.URLMessageBoard = pageName
                //    tree.URLGroup = pageName + "?Forum=CategoryList&GroupId=" & tree.Thread.Forum.Category.Group.Id
                //    tree.URLCategory = pageName + "?"
                //    tree.URLForum = pageName + "?Forum=ThreadList&ForumId=" & tree.Thread.Forum.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                // PostList
                if (Request.QueryString["Forum"] == "PostList")
                {
                    using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID))
                    {
                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
                        {
                            MessageBoard.Thread thread = MessageBoard.Thread.Get(int.Parse(Request.QueryString["ThreadId"]));
                            MessageBoardControl.PostList postList = new MessageBoardControl.PostList();
                            postList.URL = pageName + "?";
                            postList.PostSecurityUrl = pageName + "?";
                            postList.EditThreadURL = pageName + "?";
                            postList.DeleteThreadURL = pageName + "?";
                            postList.EditPostURL = pageName + "?";
                            postList.DeletePostURL = pageName + "?";
                            postList.LockThreadURL = pageName + "?";
                            postList.Thread = thread;
                            postList.CurrentUser = currentUser;
                            postList.EmotionsFolder = "/Images/Emotions";
                            postList.ImagesFolder = "/Images/Forum";
                            postList.ViewDisplayImages = true;
                            postList.DisplayImagesFolder = "/Images/DisplayImages";
                            postList.Language = currentLanguage;

                            postList.web = web;
                            contentDiv.Controls.Add(postList);

                            MessageBoardControl.AddPost addPost = new MessageBoardControl.AddPost();
                            addPost.URLAfter = pageName + "?Forum=PostList&ThreadId=" + Request.QueryString["ThreadId"];
                            addPost.Thread = MessageBoard.Thread.Get(int.Parse(Request.QueryString["ThreadId"]));
                            addPost.CurrentUser = currentUser;
                            addPost.Language = currentLanguage;
                            content2Div.Controls.Add(addPost);
                        }
                    }
                }

                // EditPost
                if (Request.QueryString["Forum"] == "EditPost")
                {
                    MessageBoard.Post post = MessageBoard.Post.Get(int.Parse(Request.QueryString["PostId"]));
                    MessageBoardControl.EditPost editPost = new MessageBoardControl.EditPost();
                    editPost.URLAfter = pageName + "?Forum=PostList&ThreadId=" + post.Thread.Id;
                    editPost.Post = post;
                    editPost.CurrentUser = currentUser;
                    editPost.Language = currentLanguage;
                    contentDiv.Controls.Add(editPost);
                }

                //// DeletePost
                //If Request.QueryString("Forum") = "DeletePost" Then
                //    Dim post As MessageBoard.Post = MessageBoard.Post.Get(CInt(Request.QueryString("PostId")))
                //    Dim deletePost As MessageBoardControl.DeletePost = New MessageBoardControl.DeletePost
                //    deletePost.URLAfter = pageName + "?Forum=PostList&ThreadId=" & post.Thread.Id
                //    deletePost.Post = post
                //    deletePost.CurrentUser = currentUser
                //    deletePost.Language = currentLanguage
                //    contentDiv.Controls.Add(deletePost)

                //    Dim tree As MessageBoardControl.Tree = New MessageBoardControl.Tree
                //    tree.ImagesFolder = "Images/Forum"
                //    tree.Thread = post.Thread
                //    tree.URLMessageBoard = pageName
                //    tree.URLGroup = pageName + "?Forum=CategoryList&GroupId=" & tree.Thread.Forum.Category.Group.Id
                //    tree.URLCategory = pageName + "?"
                //    tree.URLForum = pageName + "?Forum=ThreadList&ForumId=" & tree.Thread.Forum.Id
                //    tree.Language = currentLanguage
                //    treeDiv.Controls.Add(tree)
                //End If

                //// ManageSecurity
                if (Request.QueryString["Forum"] == "ManageSecurity")
                {
                    MessageBoardControl.ManageSecurity manageSecurity = new MessageBoardControl.ManageSecurity();
                    manageSecurity.URLAfter = pageName + "?Forum=GroupList";
                    manageSecurity.Language = currentLanguage;
                    if (!string.IsNullOrEmpty(Request.QueryString["GroupId"]))
                    {
                        manageSecurity.URLAfter = pageName + "?Forum=GroupList";
                        manageSecurity.Group = MessageBoard.Group.Get(int.Parse(Request.QueryString["GroupId"].ToString()));
                    }
                    else if (!string.IsNullOrEmpty(Request.QueryString["CategoryId"]))
                    {
                        manageSecurity.Category = MessageBoard.Category.Get(int.Parse(Request.QueryString["CategoryId"].ToString()));
                        manageSecurity.URLAfter = pageName + "?Forum=CategoryList&GroupId=" + manageSecurity.Category.Group.Id;
                    }
                    else if (!string.IsNullOrEmpty(Request.QueryString["ForumId"]))
                    {
                        manageSecurity.Forum = MessageBoard.Forum.Get(int.Parse(Request.QueryString["ForumId"].ToString()));
                        manageSecurity.URLAfter = pageName + "?Forum=CategoryList&GroupId=" + manageSecurity.Forum.Category.Group.Id;
                    }
                    else if (!string.IsNullOrEmpty(Request.QueryString["ThreadId"]))
                    {
                        manageSecurity.Thread = MessageBoard.Thread.Get(int.Parse(Request.QueryString["ThreadId"].ToString()));
                        manageSecurity.URLAfter = pageName + "?Forum=ThreadList&ForumId=" + manageSecurity.Thread.Forum.Id;
                    }
                    else if (!string.IsNullOrEmpty(Request.QueryString["PostId"]))
                    {
                        manageSecurity.Post = MessageBoard.Post.Get(int.Parse(Request.QueryString["PostId"].ToString()));
                        manageSecurity.URLAfter = pageName + "?Forum=PostList&ThreadId=" + manageSecurity.Post.Thread.Id;
                    }
                    contentDiv.Controls.Add(manageSecurity);

                    //if (Request.QueryString["GroupId"] != "")
                    //{
                    //    manageSecurity.Group = MessageBoard.Group.Get(int.Parse(Request.QueryString["GroupId"].ToString()));
                    //}
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
